﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Web;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Xml;

namespace Common
{
    public class Commons
    {
        public static XmlDocument doc;
        public static XmlNodeList nodeList;
        static FileInfo xmlfile = new FileInfo(System.Windows.Forms.Application.StartupPath + @"/config.xml");
        static Commons()
        {
            
        }
        

        
        
        public static string GetImg(string path, int size)
        {
            string[] files = path.Split('.');
            string ext = "." + files[files.Length - 1];
            return path.Replace(ext,"") + "_" + size + ext;
        }

        //字符串前后加逗号
        public static string TranDou(object str)
        {
            return str.ToString() == "" ? "" : "," + str + ",";
        }
        public static void setXmlDoc(string rootStr)
        {
            doc = new XmlDocument();
            doc.Load(xmlfile.FullName);
            nodeList = doc.SelectSingleNode(rootStr).ChildNodes;
        }
        public static string GetConfig(string name)
        {
            try
            {
                string val = "";
                Commons.setXmlDoc("root//System");
                foreach (XmlNode node in Commons.nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == name)
                    {
                        val = xe.GetAttribute("value");
                        break;
                    }
                }
                return val;

            }
            catch
            {
                return "";
            }
        }


        public static string SubString(string str, int len)
        {
            str = FilterHTML(str);
            if (str.Length > len)
            {
                return str.Substring(0, len);
            }
            else
            {
                return str;
            }
        }
        public static string SubString2(string str, int len)
        {
            if (str.Length > len)
            {
                return str.Substring(0, len);
            }
            else
            {
                return str;
            }
        }
        public static string FilterHTML(string html)
        {
            if (html == null)
            {
                html = "";
            }
            html = html.ToLower();
            System.Text.RegularExpressions.Regex regex1 =
                  new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 =
                  new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 =
                  new System.Text.RegularExpressions.Regex(@" no[sS]*=",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 =
                  new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 =
                  new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 =
                  new System.Text.RegularExpressions.Regex(@"<img[^>]+>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 =
                  new System.Text.RegularExpressions.Regex(@"</p>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 =
                  new System.Text.RegularExpressions.Regex(@"<p>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 =
                  new System.Text.RegularExpressions.Regex(@"<[^>]*>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            //html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = Regex.Replace(html, "[\f\r\t\v]", "");  //过滤回车换行制表符
            return html;
        }

       

       
        /// <summary>
        /// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
        /// </summary>
        /// <param name="DateTime1">第一个日期和时间</param>
        /// <param name="DateTime2">第二个日期和时间</param>
        /// <returns></returns>
        public static int DateDiff(DateTime DateTime1, DateTime DateTime2, string Flag)
        {
            double dateDiff = 0;
            try
            {
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                if (Flag == "D")
                {
                    dateDiff = ts.TotalDays;
                }else if (Flag == "H")
                {
                    dateDiff = ts.TotalHours;
                }
                else if (Flag == "M")
                {
                    dateDiff = ts.TotalMinutes;
                }
                else if (Flag == "S")
                {
                    dateDiff = ts.TotalSeconds;
                }

            }
            catch
            {
            }
            return (int)dateDiff;
        }

        /// <summary>
        /// 获取字符中指定标签的值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="title">标签</param>
        /// <returns>值</returns>
        public static string GetTitleContent(string str, string title)
        {
            string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title); //获取<title>之间内容
            Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
            string result = TitleMatch.Groups["Text"].Value;
            return result;
        }

        /// <summary>  
        /// 获取字符中指定标签的值  
        /// </summary>  
        /// <param name="str">字符串</param>  
        /// <param name="title">标签</param>  
        /// <param name="attrib">属性名</param>  
        /// <returns>属性</returns>  
        public static string GetAttrContent(string str, string title, string attrib)
        {
            string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", title, attrib); //获取<title>之间内容  
            Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
            string result = TitleMatch.Groups["url"].Value;
            return result;
        }

        
        

        /// <summary>
        /// 提取文本内容中的url连接
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static List<string> GetUrlByText(string text)
        {
            MatchCollection list = Regex.Matches(text, @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%#_\./-~-]*)?", RegexOptions.IgnoreCase);
            List<string> slist = new List<string>();
            foreach (Match match in list)
            {
                slist.Add(match.Value);
            }
            return slist;
        }
        public static string GetTextByTag(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"(?is)<div id=""js_article""[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>");
            Match first = reg.Match(text);
            string sss = first.Value;
            return first.Value;
        }
        public static string GetTouTiaoTextByTag(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"(?is)<div class=""article-content""[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>");
            Match first = reg.Match(text);
            return "<div id=\"js_article\" class=\"rich_media\"><div class=\"rich_media_inner\"><div id=\"page-content\"><div id=\"img-content\" class=\"rich_media_area_primary\"><div class=\"rich_media_content \" id=\"js_content\">" + first.Value + "</div></div></div></div></div>";
        }
        public static string GetKuaiBaoTextByTag(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"(?is)<div id=""content""[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>");
            Match first = reg.Match(text);
            return "<div id=\"js_article\" class=\"rich_media\"><div class=\"rich_media_inner\"><div id=\"page-content\"><div id=\"img-content\" class=\"rich_media_area_primary\"><div class=\"rich_media_content \" id=\"js_content\">" + first.Value + "</div></div></div></div></div>";
        }
        public static string GetKuaiBaoTextByTag2(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"(?is)<div class=""_1S_TjXVYjV91ADwjZWaCx1 _1D4BWrRmNahcH1KgQoDh9D""[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>");
            Match first = reg.Match(text);
            return "<div id=\"js_article\" class=\"rich_media\"><div class=\"rich_media_inner\"><div id=\"page-content\"><div id=\"img-content\" class=\"rich_media_area_primary\"><div class=\"rich_media_content \" id=\"js_content\">" + first.Value + "</div></div></div></div></div>";
        }
        public static string GetKuaiBaoTextByTag3(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"<div class=""Gs2zARsPKTd9DTv33YTwB""(\w|\W)*?<\/div>");
            Match first = reg.Match(text);
            return first.Value;
        }
        public static string GetTextByTag2(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"(?is)<div class=""rich_media_content[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>");
            Match first = reg.Match(text);
            return first.Value;
        }
        public static string GetTextByTag3(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"<p class=""title""(\w|\W)*?<\/p>");
            Match first = reg.Match(text);
            return first.Value;
        }

        public static string GetTable(string text)
        {
            // 获取前后对应的标记 如果是双引号，可以这样写：class=""left""
            Regex reg = new Regex(@"(?is)<table class=""json_data asc""[^>]*>(?><table[^>]*>(?<o>)|</table>(?<-o>)|(?:(?!</?table\b).)*)*(?(o)(?!))</table>");
            Match first = reg.Match(text);
            string sss = first.Value;
            return first.Value;
        }

        public static List<string> GetTr(string text)
        {
            MatchCollection list = Regex.Matches(text, @"<tr(\w|\W)*?<\/tr>", RegexOptions.IgnoreCase);
            List<string> slist = new List<string>();
            foreach (Match match in list)
            {
                slist.Add(match.Value);
            }
            return slist;
        }
        public static List<string> GetTd(string text)
        {
            MatchCollection list = Regex.Matches(text, @"<td(\w|\W)*?<\/td>", RegexOptions.IgnoreCase);
            List<string> slist = new List<string>();
            foreach (Match match in list)
            {
                slist.Add(match.Value);
            }
            return slist;
        }
    }
}
